#ifndef CALCLOGIC_H
#define CALCLOGIC_H
#include <QObject>
#include <QAbstractButton>
#include "Common.h"
#include "calc_var.h"
/*
This is the front end logic that handles all button presses and formats them.
It handles all button press and reacts to it accordingly.
*/

class CalcLogic : public QObject
{
    Q_OBJECT

public:
    enum OperatorType {
        None,
        Addtion,
        Substraction,
        Multiplication,
        Division,
        Percent,
        Mod,
        PlusMinus,
        Sqroot,
        Reciprocal,
        ClearOperand,
        ClearEverything,
        Dot,
        Equal,
        Backspace,
        MemoryPlus,
        MemoryMinus,
        MemoryStore,
        MemoryRead,
        MemoryClear,
        sinvalue,
        cosvalue,
        tanvalue,
        sinhvalue,
        coshvalue,
        tanhvalue,
        power,
        square,
        cube,
        logvalue,
        factorial,
        cuberoot,
        Tenpowerx,
        yrootofx,
        pivalue,
        mantissa,
        dmsvalue,
        naturalLog,
        exponentialvalue,
        asinvalue,
        acosvalue,
        atanvalue,
        asinhvalue,
        acoshvalue,
        atanhvalue,
        epowerx,
        todegress,
        twopivalue,
        Intvalue,
        Fracvalue,
        BitwiseNot,
        BitwiseAnd,
        LShift,
        Rshift,
        BitwiseOr,
        BitwiseXor,
        RotateLeft,
        RotateRight,
        AddToDataset //Stastics mode
    };

    explicit CalcLogic(QObject *parent = 0);
    void setMode(CalcMode mode);
    CalcMode mode();
    UnitsType unitsType();

    QString convertDecToDec(int decimalValue);
    QString convertDecToBin(int decimalValue);
    QString convertDecToOct(int decimalValue);
    QString convertDecToHex(int decimalValue);
    QString convertDecToTargetRadix(int decimalValue, RadixType radix);

signals:
    void resultAvailable(QString);
    void byteResultAvailable(QString);
    void updateBraceCounter(QString);
    void addDatasetItem(QString);
    void clearDataset();

public slots:
    void buttonPressed(QAbstractButton* button);
    void degreesSelected();
    void radiansSelected();
    void gradsSelected();
    void openbraceSelected();
    void closebraceSelected();

private slots:
    void handleEqualTo();
    void handleDecClicked();
    void handleBinClicked();
    void handleOctClicked();
    void handleHexClicked();
    void handleQWordClicked();
    void handleDWordClicked();
    void handleWordClicked();
    void handleByteClicked();
   // void handleAlphabetsclicked();
    void handleInputInProgramMode(QString inputString);

private:
    double evaluateExpression(QString expression);
    void formatAndUpdateConsole(QString userinput,double calulationResult);
    void formatAndUpdateByteConsole(double input, WordType conversionTo);
    void clearEverything();
    QString functionNameForOperator(OperatorType functionType,UnitsType type);

private:
    bool isDigit(QString text);
    bool isHexAlphabet(QString text);
    OperatorType operatorType(QString text);
    QString mUserInput;
    QString mUserInputOperand;
    CalcMode mCalcMode;
    OperatorType mCurrentOperatorType;
    UnitsType mCurrentUnitsType;
    WordType mWordType;
    RadixType mRadixType;
    int mDecimalValue;
    double op1;
    double op2;
    double op3;
    double units;
    QString ch;
    double mvalueinMemory;
    QString mStr;
    int mBraceCounter;
    Calc_var* calculator;
    double mCurrentResult; // result from current user input
    QString mTextToDisplayOnConsole;
    QList<int> mStatisticsDataset;
};

#endif // CALCLOGIC_H
